package com.dcbeida.activity.userinfo;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.bean.User;
import com.dcbeida.bean.UserInfo;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.JsonGet;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 150410 on 2017/8/31.
 */

public class UserInfoPersenter implements UserInfoContract.Persent {
    private final ModelUtils model;
    UserInfoContract.View view;
    Activity activity;

    public UserInfoPersenter(UserInfoContract.View view, Activity activity) {
        this.view = view;
        this.activity = activity;
        model = ModelUtils.newInstance();
    }

    @Override
    public void start() {

    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };

    @Override
    public void setData(String user, String customerservice) {
        JsonGet jg = new JsonGet().putopt("uid", MyApplication.user.getUid())
                .putopt("shopid", MyApplication.user.getShopid())
                .putopt("customerservice", customerservice)
                .putopt("user", user);
        String json = jg.tostring();
        model.getDate("updateuserinfo", json, new UserInfoPersenter.GetListCallBack(activity, User.class, lis));
    }

    @Override
    public void getUserInfo(String uid, String userid) {
        JsonGet jg = new JsonGet().putopt("uid", MyApplication.user.getUid());
        String json = jg.tostring();
        model.getDate("getuserinfo", json, new UserInfoPersenter.GetUserInfoCallBack(activity, UserInfo.class, lis));
    }

    public class GetListCallBack<T> extends DialogCallback<T> {
        public GetListCallBack(Activity activity, Type type, DialogInterface.OnCancelListener lis) {
            super(activity, type, lis);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                view.showTip(activity.getString(R.string.xgcg));
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");
        }
    }

    public class GetUserInfoCallBack<T> extends DialogCallback<T> {
        public GetUserInfoCallBack(Activity activity, Type type, DialogInterface.OnCancelListener lis) {
            super(activity, type, lis);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                view.showUserInfo((UserInfo)data);
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");
        }
    }
}
